//
//  GLFunView.m
//  GLFun
//
//  Created by TTKai on 11-8-23.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "GLFunView.h"
#import "UIColor-Random.h"

@interface GLFunView()

- (void)draw;

@end

@implementation GLFunView
@synthesize firstTouch;
@synthesize lastTouch;
@synthesize currentColor;
@synthesize useRandomColor;
@synthesize shapeType;
@synthesize sprite;


- (id)initWithCoder:(NSCoder *)aDecoder
{
	if (self = [super initWithCoder:aDecoder])
	{
		self.currentColor = [UIColor redColor];
		self.useRandomColor = NO;
		
		timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerCome) userInfo:nil repeats:NO];
	}
	return self;
}

- (void)timerCome
{
	self.currentColor = [UIColor randomColor];
	[self draw];
}

- (void)draw
{
	glLoadIdentity();
	glClearColor(0.78f, 0.78f, 0.78f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT);
	
	CGColorRef color = currentColor.CGColor;
	const CGFloat *components = CGColorGetComponents(color);
	CGFloat red = components[0];
	CGFloat green = components[1];
	CGFloat blue = components[2];
	
	glDisable(GL_BLEND);
	
	glColor4f(red, green, blue, 1.0);
	
	switch (shapeType)
	{
		case KLineShape:
		{
			if (sprite != nil)
			{
				[sprite release];
				self.sprite = nil;
			}
			
			GLfloat vertices[4];
			vertices[0] = firstTouch.x;
			vertices[1] = self.frame.size.height - firstTouch.y;
			vertices[2] = lastTouch.x;
			vertices[3] = self.frame.size.height - lastTouch.y;
			glLineWidth(2.0);
			glVertexPointer(2, GL_FLOAT, 0, vertices);
			glDrawArrays(GL_LINES, 0, 2);
			
			break;
		}
			
		case KRectShape:
		{
			if (sprite != nil)
			{
				[sprite release];
				self.sprite = nil;
			}
			GLfloat vertices[8];
			GLfloat minX = (firstTouch.x > lastTouch.x) ? lastTouch.x : firstTouch.x;
			GLfloat maxX = (firstTouch.x > lastTouch.x) ? firstTouch.x : lastTouch.x;
			GLfloat tmpY1 = self.frame.size.height - firstTouch.y;
			GLfloat tmpY2 = self.frame.size.height - lastTouch.y;
			GLfloat minY = (tmpY1 > tmpY2) ? tmpY2 : tmpY1;
			GLfloat maxY = (tmpY1 > tmpY2) ? tmpY1 : tmpY2;
			vertices[0] = minX;
			vertices[1] = minY;
			vertices[2] = maxX;
			vertices[3] = minY;
			vertices[4] = maxX;
			vertices[5] = maxY;
			vertices[6] = minX;
			vertices[7] = maxY;
			
			glVertexPointer(2, GL_FLOAT, 0, vertices);
			glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
			break;
		}
			
		case KEllipseShape:
		{
			if (sprite != nil)
			{
				[sprite release];
				self.sprite = nil;
			}
			GLfloat vertices[720];
			GLfloat xradius = (firstTouch.x > lastTouch.x) ? (firstTouch.x - lastTouch.x) / 2 : (lastTouch.x - firstTouch.x) / 2;
			GLfloat tmpY1 = self.frame.size.height - firstTouch.y;
			GLfloat tmpY2 = self.frame.size.height - lastTouch.y;
			GLfloat yradius = (tmpY1 > tmpY2) ? (tmpY1 - tmpY2) / 2 : (tmpY2 - tmpY1) / 2;
			CGFloat xOffset = (firstTouch.x > lastTouch.x) ? lastTouch.x + xradius : firstTouch.x + xradius;
			CGFloat yOffset = (tmpY1 > tmpY2) ? tmpY2 + yradius : tmpY1 + yradius;
			for (int i=0; i<=720; i+=2)
			{
				vertices[i] = (cos(degreesToRadian(i>>1)) * xradius) + xOffset;
				vertices[i+1] = (sin(degreesToRadian(i>>1)) * yradius) + yOffset;
			}
			glVertexPointer(2, GL_FLOAT, 0, vertices);
			glDrawArrays(GL_TRIANGLE_FAN, 0, 360);
			break;
		}
			
		case KImageShape:
		{
			glEnable(GL_TEXTURE_2D);
			if (sprite == nil)
			{
				UIImage* img = [UIImage imageNamed:@"jingtian.png"];
				self.sprite = [[Texture2D alloc] initWithImage:img];
				glBindTexture(GL_TEXTURE_2D, sprite.name);
			}
			[sprite drawAtPoint:CGPointMake(lastTouch.x, self.frame.size.height - lastTouch.y)];
			break;
		}
			
		default:
			;
	}
	
	UIFont* font = [UIFont systemFontOfSize:18];
	NSString* text = @"阿拉测试文字Test我是who啊you侬";
	CGSize textSize = [text sizeWithFont:font];
	Texture2D* tmpTest = [[Texture2D alloc] initWithString:text dimensions:textSize alignment:UITextAlignmentLeft fontName:font.fontName fontSize:18];
//	glBindTexture(GL_TEXTURE_2D, tmpTest.name);
	glEnable(GL_TEXTURE_2D);
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//	[tmpTest drawAtPoint:CGPointMake(firstTouch.x + textSize.width / 2, self.frame.size.height - firstTouch.y + textSize.height / 2)];
//	textSize.width += 100;
//	textSize.height += 50;
//	CGRect testRect = CGRectMake(50, self.frame.size.height - 60 - 80, textSize.width, textSize.height);
//	CGRect testRect = CGRectMake(20, self.frame.size.height - 40, textSize.width, textSize.height);
//	[tmpTest drawInRect:testRect];
//	testRect.origin.y -= 30;
//	[tmpTest drawInRect:testRect];
//	testRect.origin.y -= 30;
//	[tmpTest drawInRect:testRect];
//	testRect.origin.y -= 30;
//	[tmpTest drawInRect:testRect];
//	testRect.origin.y -= 30;
//	[tmpTest drawInRect:testRect];
//	testRect.origin.y -= 30;
//	[tmpTest drawInRect:testRect];
//	testRect.origin.y -= 30;
//	[tmpTest drawInRect:testRect];
//	testRect.origin.y -= 30;
//	[tmpTest drawInRect:testRect];
//	testRect.origin.y -= 30;
//	[tmpTest drawInRect:testRect];
//	testRect.origin.y -= 30;
//	[tmpTest drawInRect:testRect];
//	testRect.origin.y -= 30;
//	[tmpTest drawInRect:testRect];
//	testRect.origin.y -= 30;
//	[tmpTest drawInRect:testRect];
//	testRect.origin.y -= 30;
//	[tmpTest drawInRect:testRect];
	CGPoint pointPos = CGPointMake(20 + textSize.width / 2, self.frame.size.height - 40);
	[tmpTest drawAtPoint:pointPos];
	pointPos.y -= 30;
	[tmpTest drawAtPoint:pointPos];
	pointPos.y -= 30;
	[tmpTest drawAtPoint:pointPos];
	pointPos.y -= 30;
	[tmpTest drawAtPoint:pointPos];
	pointPos.y -= 30;
	[tmpTest drawAtPoint:pointPos];
	pointPos.y -= 30;
	[tmpTest drawAtPoint:pointPos];
	pointPos.y -= 30;
	[tmpTest drawAtPoint:pointPos];
	pointPos.y -= 30;
	[tmpTest drawAtPoint:pointPos];
	pointPos.y -= 30;
	[tmpTest drawAtPoint:pointPos];
	pointPos.y -= 30;
	[tmpTest drawAtPoint:pointPos];
	pointPos.y -= 30;
	[tmpTest drawAtPoint:pointPos];
	pointPos.y -= 30;
	[tmpTest drawAtPoint:pointPos];
	pointPos.y -= 30;
	[tmpTest drawAtPoint:pointPos];
		
	[tmpTest release];
	
	glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
	[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

- (void)dealloc
{
	[timer invalidate];
	[currentColor release];
	[sprite release];
	
	[super dealloc];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	if (useRandomColor)
	{
		self.currentColor = [UIColor randomColor];
	}
	
	UITouch* touch = [[event touchesForView:self] anyObject];
	firstTouch = [touch locationInView:self];
	lastTouch = [touch locationInView:self];
	
	[self draw];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch* touch = [touches anyObject];
	lastTouch = [touch locationInView:self];
	
	[self draw];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch* touch = [touches anyObject];
	lastTouch = [touch locationInView:self];
	
	[self draw];
}

@end
